//
//  ZJRBButtonsView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/14.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJRBButtonsView.h"
#import "ZJNeedAndSpecialityModel.h"
@interface ZJRBButtonsView ()
{
    NSArray *_dataArray;/**< 传进来的数据 */
     BOOL _isCheck;/**< 是否是党员审核中 */
}
@end

@implementation ZJRBButtonsView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame title:title];
    if (self) {
        
    }
    return self;
}

- (void)updateUI_dataArray:(NSArray *)dataArray seleArray:(NSArray *)seleArray{
    [self updateUI_dataArray:dataArray seleArray:seleArray isCheck:NO];
}
- (void)updateUI_dataArray:(NSArray *)dataArray seleArray:(NSArray *)seleArray isCheck:(BOOL)isCheck{
    _isCheck = isCheck;
    _dataArray = dataArray;
    [self.seletArray removeAllObjects];
    if (_dataArray.count < 1) {
        self.height = 0;
        if (self.delegate) {
            [self.delegate ZJRBButtonsViewDelegate_View:self];
        }
        return;
    }
    for (id view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view removeFromSuperview];
        }
    }
    CGFloat maxHeight = self.titleLabel.bottom;
    
    CGFloat top = self.titleLabel.bottom;
    CGFloat left = self.titleLabel.x;
    CGFloat spaceW = 10 * [UIScreen zj_screenWScale];
    CGFloat spaceH = 12 * [UIScreen zj_screenWScale];
    
    CGFloat buttonW = (self.width - 2 * left - 2 * spaceW) / 3;
    CGFloat buttonH = 35 * [UIScreen zj_screenHScale];
    
    for (int i = 0; i < dataArray.count; i++) {
        ZJNeedAndSpecialityModel *model = dataArray[i];
        NSInteger zheng = i / 3;
        NSInteger yu = i % 3;
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(left + yu * (buttonW + spaceW), (buttonH + spaceH) * zheng + top, buttonW, buttonH)];
        
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor zj_colorWithHexString:@"a8a8a8"].CGColor;
        button.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
        button.tag = i;
        button.titleLabel.font = [UIFont titleTextFont];
        [button setTitle:model.CODE_NAME forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        if ([seleArray containsObject:model.CODE_VALUE]) {
            [self setButton:button isSelect:YES];
            [self.seletArray addObject:model];
        }else{
            [self setButton:button isSelect:NO];
            [self.seletArray removeObject:model];
        }
        
        maxHeight = button.bottom;
        [self addSubview:button];
        [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.height = maxHeight + 6 * [UIScreen zj_screenHScale];
    if (self.delegate) {
        [self.delegate ZJRBButtonsViewDelegate_View:self];
    }
}


- (void)action_buttonClick:(UIButton *)sender{
    if (sender.selected) {//取消选中
        [self setButton:sender isSelect:NO];
        [self.seletArray removeObject:_dataArray[sender.tag]];
    }else{
        [self setButton:sender isSelect:YES];
        [self.seletArray addObject:_dataArray[sender.tag]];
    }

}

- (void)setButton:(UIButton *)button isSelect:(BOOL)isSelect{
    button.selected = isSelect;
    if (isSelect) {//选中
        if (_isCheck) {//党员报到审核中
            button.backgroundColor = [UIColor themeGrayColor];
        }else{
            button.backgroundColor = [UIColor themeColor];
        }
        button.layer.borderWidth = 0;
    }else{//未选中
        button.backgroundColor = [UIColor clearColor];
        button.layer.borderWidth = 1;
    }
}


- (NSMutableArray *)seletArray{
    if (_seletArray == nil){
        _seletArray = [NSMutableArray array];
        
    }
    return _seletArray;
}

@end
